home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libmpeg_src.lha / fs2fast.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-25  |  7.2 KB  |  294 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. #include <config.h>
  22. #include "video.h"
  23. #include "proto.h"
  24. #include "dither.h"
  25.  
  26. /* Arrays containing error values for floyd-steinberg dithering. */
  27.  
  28. static int deltay[256];
  29. static int deltau[256];
  30. static int deltav[256];
  31. static int deltay2[256];
  32. static int deltau2[256];
  33. static int deltav2[256];
  34.  
  35. /* Definitions governing number of bits used for luminance, cr, and cb. */
  36.  
  37. #define L_BITS 3
  38. #define CR_BITS 2
  39. #define CB_BITS 2
  40.  
  41. /* Masks for proper quantization of lum, cr, and cb values. */
  42.  
  43. #define L_MASK 0xe0
  44. #define CR_MASK 0xc0
  45. #define CB_MASK 0xc0
  46.  
  47.  
  48.  
  49. /*
  50.  *--------------------------------------------------------------
  51.  *
  52.  * InitFS2FastDither --
  53.  *
  54.  *    Initializes structures and arrays neeeded for fast implementation
  55.  *      of two error F-S dithering.
  56.  *
  57.  * Results:
  58.  *    None.
  59.  *
  60.  * Side effects:
  61.  *      None.
  62.  *
  63.  *--------------------------------------------------------------
  64.  */
  65.  
  66. void InitFS2FastDither()
  67. {
  68.   int i;
  69.   int lum_num, cr_num, cb_num;
  70.  
  71.   for (i=0; i<256; i++) {
  72.     lum_num = (i >> (8-L_BITS));
  73.     cr_num = (i >> (8-CR_BITS));
  74.     cb_num = (i >> (8-CB_BITS));
  75.  
  76.     /* These arrays contain the error values propogated for each pixel value 
  77.        for each channel. 
  78.     */
  79.  
  80.     deltay[i] = (i - ((int) lum_values[lum_num])) / 2;
  81.     deltau[i] = (i-((int) cr_values[cr_num])) / 2;
  82.     deltav[i] = (i-((int) cb_values[cb_num])) / 2;
  83.     deltay2[i] = (i - ((int) lum_values[lum_num])) - deltay[i];
  84.     deltau2[i] = (i - ((int) cr_values[cr_num])) - deltau[i];
  85.     deltav2[i] = (i - ((int) cb_values[cb_num])) - deltav[i];
  86.  
  87.   }
  88.  
  89. }
  90.  
  91. /*
  92.  *--------------------------------------------------------------
  93.  *
  94.  * DitherImage --
  95.  *
  96.  *    Dithers an image using floyd-steinberg.
  97.  *    Assumptions made:
  98.  *      1) The color space is allocated y:cr:cb = 8:4:4
  99.  *      2) The spatial resolution of y:cr:cb is 4:1:1
  100.  *
  101.  * Results:
  102.  *    None.
  103.  *
  104.  * Side effects:
  105.  *    None.
  106.  *
  107.  *--------------------------------------------------------------
  108.  */
  109. void
  110. FS2FastDitherImage (lum, cr, cb, out, h, w)
  111.     unsigned char *lum;
  112.     unsigned char *cr;
  113.     unsigned char *cb;
  114.     unsigned char *out;
  115.     int w, h;
  116. {
  117.     int i, j, idx, idx2;
  118.     int y, u, v;
  119.     int dy, du, dv;
  120.     int code;
  121.     static int *yerr1;
  122.     static int *yerr2;
  123.     static int *uerr1;
  124.     static int *uerr2;
  125.     static int *verr1;
  126.     static int *verr2;
  127.     int *ye1, *ue1, *ve1;
  128.     int *ye2, *ue2, *ve2;
  129.     unsigned char *o, *l, *r, *b;
  130.     static int first = 1;
  131.  
  132.     /* If first time called, allocate error arrays. */
  133.  
  134.     if (first) {
  135.       first = 0;
  136.       yerr1 = (int *) malloc((w+5)*sizeof(int));
  137.       yerr2 = (int *) malloc((w+5)*sizeof(int));
  138.       uerr1 = (int *) malloc((w+5)*sizeof(int));
  139.       uerr2 = (int *) malloc((w+5)*sizeof(int));
  140.       verr1 = (int *) malloc((w+5)*sizeof(int));
  141.       verr2 = (int *) malloc((w+5)*sizeof(int));
  142.     }
  143.  
  144.     /*
  145.      * Init error arrays and variables.
  146.      */
  147.     memset ((char *)yerr1, 0, (w+5)*sizeof(int));
  148.     memset ((char *)yerr2, 0, (w+5)*sizeof(int));
  149.     memset ((char *)uerr1, 0, (w+5)*sizeof(int));
  150.     memset ((char *)uerr2, 0, (w+5)*sizeof(int));
  151.     memset ((char *)verr1, 0, (w+5)*sizeof(int));
  152.     memset ((char *)verr2, 0, (w+5)*sizeof(int));
  153.     du = dv = dy = 0;
  154.  
  155.     for (j=0; j<h; j+=2) {
  156.     ye1 = yerr1;
  157.     ue1 = uerr1;
  158.     ve1 = verr1;
  159.     ye2 = yerr2;
  160.     ue2 = uerr2;
  161.     ve2 = verr2;
  162.     idx = j*w;
  163.     idx2 = idx/4;
  164.     o = out+idx;
  165.     l = lum+idx;
  166.     r = cr+idx2;
  167.     b = cb+idx2;
  168.     /* Do the top row in forward order. */
  169.     for (i=0; i<w; i+=2) {
  170.         /* Do left side of this pair... */
  171.         y = *l++ + dy + *ye1++;
  172.         u = *r + du + *ue1++;
  173.         v = *b + dv + *ve1++;
  174.  
  175.         if (y < 0) y = 0;
  176.         else if (y > 255) y = 255;
  177.         if (u < 0) u = 0;
  178.         else if (u > 255) u = 255;
  179.         if (v < 0) v = 0;
  180.         else if (v > 255) v = 255;
  181.  
  182.         /*
  183.          * Construct a code using:
  184.          *    high order 3 bits of y, 
  185.          *    high order 2 bits of u, 
  186.          *    high order 2 bits of v
  187.          */
  188.         code = (((y & L_MASK) | ((u & CR_MASK) >> L_BITS) | (v >> (L_BITS+CR_BITS))) 
  189.             >> (8-(L_BITS+CR_BITS+CB_BITS)));
  190.         *o++ = pixel[code];
  191.         *ye2++ = deltay[y];
  192.         *ue2++ = deltau[u];
  193.         *ve2++ = deltav[v];
  194.         dy = deltay2[y];
  195.         du = deltau2[u];
  196.         dv = deltav2[v];
  197.  
  198.         /* Do right side of this pair... */
  199.         y = *l++ + dy + *ye1++;
  200.         u = *r++ + du + *ue1++;
  201.         v = *b++ + dv + *ve1++;
  202.  
  203.         if (y < 0) y = 0;
  204.         else if (y > 255) y = 255;
  205.         if (u < 0) u = 0;
  206.         else if (u > 255) u = 255;
  207.         if (v < 0) v = 0;
  208.         else if (v > 255) v = 255;
  209.  
  210.         code = (((y & L_MASK) | ((u & CR_MASK) >> L_BITS) | (v >> (L_BITS+CR_BITS))) 
  211.             >> (8-(L_BITS+CR_BITS+CB_BITS)));
  212.         *o++ = pixel[code];
  213.         *ye2++ = deltay[y];
  214.         *ue2++ = deltau[u];
  215.         *ve2++ = deltav[v];
  216.         dy = deltay2[y];
  217.         du = deltau2[u];
  218.         dv = deltav2[v];
  219.  
  220.     }
  221.     
  222.     ye1 = yerr1+w-1;
  223.     ue1 = uerr1+w-1;
  224.     ve1 = verr1+w-1;
  225.     ye2 = yerr2+w-1;
  226.     ue2 = uerr2+w-1;
  227.     ve2 = verr2+w-1;
  228.     l += w-1;
  229.     o += w-1;
  230.     r--;
  231.     b--;
  232.     dy = du = dv = 0;
  233.  
  234.     /* Do bottom part of row, in right to left order. */
  235.     for (i=w-1; i>0; i-=2) {
  236.         /* Do right side of this pair... */
  237.         y = *l-- + dy + *ye2--;
  238.         u = *r + du + *ue2--;
  239.         v = *b + dv + *ve2--;
  240.  
  241.              if (y < 0) y = 0;
  242.         else if (y > 255) y = 255;
  243.         if (u < 0) u = 0;
  244.         else if (u > 255) u = 255;
  245.         if (v < 0) v = 0;
  246.         else if (v > 255) v = 255;
  247.  
  248.         /*
  249.          * Construct a code using:
  250.          *    high order 3 bits of y, 
  251.          *    high order 2 bits of u, 
  252.          *    high order 2 bits of v
  253.          */
  254.         code = (((y & L_MASK) | ((u & CR_MASK) >> L_BITS) | (v >> (L_BITS+CR_BITS))) 
  255.             >> (8-(L_BITS+CR_BITS+CB_BITS)));
  256.         *o-- = pixel[code];
  257.         *ye1-- = deltay[y];
  258.         *ue1-- = deltau[u];
  259.         *ve1-- = deltav[v];
  260.         dy = deltay2[y];
  261.         du = deltau2[u];
  262.         dv = deltav2[v];
  263.  
  264.         /* Do left side of this pair... */
  265.         y = *l-- + dy + *ye2--;
  266.         u = *r-- + du + *ue2--;
  267.         v = *b-- + dv + *ve2--;
  268.  
  269.         if (y < 0) y = 0;
  270.         else if (y > 255) y = 255;
  271.         if (u < 0) u = 0;
  272.         else if (u > 255) u = 255;
  273.         if (v < 0) v = 0;
  274.         else if (v > 255) v = 255;
  275.  
  276.         code = (((y & L_MASK) | ((u & CR_MASK) >> L_BITS) | (v >> (L_BITS+CR_BITS))) 
  277.             >> (8-(L_BITS+CR_BITS+CB_BITS)));
  278.         *o-- = pixel[code];
  279.         *ye1-- = deltay[y];
  280.         *ue1-- = deltau[u];
  281.         *ve1-- = deltav[v];
  282.         dy = deltay2[y];
  283.         du = deltau2[u];
  284.         dv = deltav2[v];
  285.  
  286.     }
  287.     }
  288. }
  289.  
  290.  
  291.  
  292.  
  293.  
  294.